package com.dou.serviceimpl;

import com.dou.dao.*;
import com.dou.entity.AppInfo;
import com.dou.entity.PageBean;
import com.dou.service.AppInfoService;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Appinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoDao appInfoDao;
    @Autowired
    private DataDictionaryDao dataDictionaryDao;
    @Autowired
    private AppVersionDao appVersionDao;
    @Autowired
    private AppCategoryDao appCategoryDao;
    @Override
    public PageBean<AppInfo> selectInfo(AppInfo appInfo,int currentPage) {
        PageBean<AppInfo> pageBean = new PageBean<AppInfo>();
        pageBean.setCurrPage(currentPage);
        int pageSize = 3;
        pageBean.setPageSize(pageSize);

        int start =(currentPage-1)*pageSize;
        int size=pageBean.getPageSize();

        int totalCount = appInfoDao.selectCount(appInfo);
        pageBean.setTotalCount(totalCount);

        double tc = totalCount;
        Double num = Math.ceil(tc/pageSize);
        pageBean.setTotalPage(num.intValue());

        List<AppInfo> appInfos =  appInfoDao.selectInfo(appInfo,start,size);
        pageBean.setLists(appInfos);

        if (appInfos.size()>0){

            for (int i = 0; i <appInfos.size();i++) {
                if(appInfos.get(i).getVersionId()!=null){
                    //获取版本号
                    appInfos.get(i).setVersionNo(appVersionDao.selectVersionNo(appInfos.get(i).getVersionId()));
                }
                if(appInfos.get(i).getStatus()!=null){
                    //获取状态名称
                    appInfos.get(i).setStatusName(dataDictionaryDao.selectStatusName(appInfos.get(i).getStatus()));
                }
                if(appInfos.get(i).getFlatformId()!=null){
                    //获取平台名称
                    appInfos.get(i).setFlatformName(dataDictionaryDao.selectFlatFormName(appInfos.get(i).getFlatformId()));
                }
                if(appInfos.get(i).getCategoryLevel1()!=null){
                    //获取一级分类名称
                    appInfos.get(i).setCategoryLevel1Name(appCategoryDao.selectCateGoryLevel1(appInfos.get(i).getCategoryLevel1()));
                }
                if(appInfos.get(i).getCategoryLevel2()!=null){
                    //获取二级分类名称
                    appInfos.get(i).setCategoryLevel2Name(appCategoryDao.selectCateGoryLevel2(appInfos.get(i).getCategoryLevel2()));
                }
                if(appInfos.get(i).getCategoryLevel3()!=null){
                    //获取三级分类名称
                    appInfos.get(i).setCategoryLevel3Name(appCategoryDao.selectCateGoryLevel3(appInfos.get(i).getCategoryLevel3()));
                }
            }
        }
        return pageBean;
    }
    //判断APK唯一性
    @Override
    public AppInfo selectAPKName(String APKName) {
        return appInfoDao.selectAPKName(APKName);
    }
    //新增APP
    @Override
    public int addInfo(AppInfo appInfo) {return appInfoDao.addInfo(appInfo); }


}
